

<!DOCTYPE html>
<html lang="zh-CN" data-default-color-scheme=auto>



<head>
  <meta charset="UTF-8">
  <link rel="apple-touch-icon" sizes="76x76" href="/img/favicon.png">
  <link rel="icon" href="/img/favicon.png">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=5.0, shrink-to-fit=no">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  
  <meta name="theme-color" content="#2f4154">
  <meta name="author" content="Cheney">
  <meta name="keywords" content="Coding">
  
    <meta name="description" content="设计、编制并调试一个词法分析程序，加深对词法分析原理的理解。通过简单字符匹配实现。">
<meta property="og:type" content="article">
<meta property="og:title" content="类 Pascal语言词法分析实验（简易版）">
<meta property="og:url" content="https://cheney822.gitee.io/2021/04/05/%E7%BC%96%E8%AF%91%E5%8E%9F%E7%90%86__%E7%B1%BB%20Pascal%E8%AF%AD%E8%A8%80%E8%AF%8D%E6%B3%95%E5%88%86%E6%9E%90%E5%AE%9E%E9%AA%8C%EF%BC%88%E7%AE%80%E6%98%93%E7%89%88%EF%BC%89/index.html">
<meta property="og:site_name" content="Cheney blog">
<meta property="og:description" content="设计、编制并调试一个词法分析程序，加深对词法分析原理的理解。通过简单字符匹配实现。">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://imgbed.cheney.cc/picgo/2021040515355729.png">
<meta property="article:published_time" content="2021-04-05T00:22:00.000Z">
<meta property="article:modified_time" content="2022-04-05T13:14:27.327Z">
<meta property="article:author" content="Cheney">
<meta property="article:tag" content="C++">
<meta property="article:tag" content="编译原理">
<meta name="twitter:card" content="summary_large_image">
<meta name="twitter:image" content="https://imgbed.cheney.cc/picgo/2021040515355729.png">
  
  
  <title>类 Pascal语言词法分析实验（简易版） - Cheney blog</title>

  <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4/dist/css/bootstrap.min.css" />


  <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/github-markdown-css@4/github-markdown.min.css" />
  <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/hint.css@2/hint.min.css" />

  
    
    
      
      <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/highlight.js@10/styles/github-gist.min.css" />
    
  

  
    <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3/dist/jquery.fancybox.min.css" />
  


<!-- 主题依赖的图标库，不要自行修改 -->

<link rel="stylesheet" href="//at.alicdn.com/t/font_1749284_ba1fz6golrf.css">



<link rel="stylesheet" href="//at.alicdn.com/t/font_1736178_lbnruvf0jn.css">


<link  rel="stylesheet" href="/css/main.css" />

<!-- 自定义样式保持在最底部 -->


  <script id="fluid-configs">
    var Fluid = window.Fluid || {};
    var CONFIG = {"hostname":"cheney822.gitee.io","root":"/","version":"1.8.14","typing":{"enable":true,"typeSpeed":70,"cursorChar":"_","loop":false},"anchorjs":{"enable":true,"element":"h1,h2,h3,h4,h5,h6","placement":"right","visible":"hover","icon":""},"progressbar":{"enable":true,"height_px":3,"color":"#29d","options":{"showSpinner":false,"trickleSpeed":100}},"copy_btn":true,"image_zoom":{"enable":true,"img_url_replace":["",""]},"toc":{"enable":true,"headingSelector":"h1,h2,h3,h4,h5,h6","collapseDepth":1},"lazyload":{"enable":true,"loading_img":"/img/loading.gif","onlypost":false,"offset_factor":2},"web_analytics":{"enable":false,"baidu":null,"google":null,"gtag":null,"tencent":{"sid":null,"cid":null},"woyaola":null,"cnzz":null,"leancloud":{"app_id":null,"app_key":null,"server_url":null,"path":"window.location.pathname","ignore_local":false}},"search_path":"/local-search.xml"};
  </script>
  <script  src="/js/utils.js" ></script>
  <script  src="/js/color-schema.js" ></script>
<meta name="generator" content="Hexo 5.4.1"></head>


<body>
  <header style="height: 70vh;">
    <nav id="navbar" class="navbar fixed-top  navbar-expand-lg navbar-dark scrolling-navbar">
  <div class="container">
    <a class="navbar-brand" href="/">
      <strong>Cheney Blog</strong>
    </a>

    <button id="navbar-toggler-btn" class="navbar-toggler" type="button" data-toggle="collapse"
            data-target="#navbarSupportedContent"
            aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
      <div class="animated-icon"><span></span><span></span><span></span></div>
    </button>

    <!-- Collapsible content -->
    <div class="collapse navbar-collapse" id="navbarSupportedContent">
      <ul class="navbar-nav ml-auto text-center">
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/">
                <i class="iconfont icon-home-fill"></i>
                首页
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/archives/">
                <i class="iconfont icon-archive-fill"></i>
                归档
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/categories/">
                <i class="iconfont icon-category-fill"></i>
                分类
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/tags/">
                <i class="iconfont icon-tags-fill"></i>
                标签
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/about/">
                <i class="iconfont icon-user-fill"></i>
                关于
              </a>
            </li>
          
        
        
          <li class="nav-item" id="search-btn">
            <a class="nav-link" target="_self" href="javascript:;" data-toggle="modal" data-target="#modalSearch" aria-label="Search">
              &nbsp;<i class="iconfont icon-search"></i>&nbsp;
            </a>
          </li>
        
        
          <li class="nav-item" id="color-toggle-btn">
            <a class="nav-link" target="_self" href="javascript:;" aria-label="Color Toggle">&nbsp;<i
                class="iconfont icon-dark" id="color-toggle-icon"></i>&nbsp;</a>
          </li>
        
      </ul>
    </div>
  </div>
</nav>

    <div class="banner" id="banner" parallax=true
         style="background: url('https://imgbed.cheney.cc/Blog_config/default.png') no-repeat center center;
           background-size: cover;">
      <div class="full-bg-img">
        <div class="mask flex-center" style="background-color: rgba(0, 0, 0, 0.3)">
          <div class="page-header text-center fade-in-up">
            <span class="h2" id="subtitle" title="类 Pascal语言词法分析实验（简易版）">
              
            </span>

            
              <div class="mt-3">
  
  
    <span class="post-meta">
      <i class="iconfont icon-date-fill" aria-hidden="true"></i>
      <time datetime="2021-04-05 08:22" pubdate>
        2021年4月5日 早上
      </time>
    </span>
  
</div>

<div class="mt-1">
  
    <span class="post-meta mr-2">
      <i class="iconfont icon-chart"></i>
      4.6k 字
    </span>
  

  
    <span class="post-meta mr-2">
      <i class="iconfont icon-clock-fill"></i>
      
      
      23 分钟
    </span>
  

  
  
</div>

            
          </div>

          
        </div>
      </div>
    </div>
  </header>

  <main>
    
      

<div class="container-fluid nopadding-x">
  <div class="row nomargin-x">
    <div class="d-none d-lg-block col-lg-2"></div>
    <div class="col-lg-8 nopadding-x-md">
      <div class="container nopadding-x-md" id="board-ctn">
        <div class="py-5" id="board">
          <article class="post-content mx-auto">
            <!-- SEO header -->
            <h1 style="display: none">类 Pascal语言词法分析实验（简易版）</h1>
            
            <div class="markdown-body">
              <h4 id="一、-实验目的"><a href="#一、-实验目的" class="headerlink" title="一、 实验目的"></a>一、 实验目的</h4><p>设计、编制并调试一个词法分析程序，加深对词法分析原理的理解。</p>
<h4 id="二、-实验要求"><a href="#二、-实验要求" class="headerlink" title="二、 实验要求"></a>二、 实验要求</h4><h5 id="2-1-待分析的简单的词法"><a href="#2-1-待分析的简单的词法" class="headerlink" title="2.1 待分析的简单的词法"></a>2.1 待分析的简单的词法</h5><h6 id="（1）关键字："><a href="#（1）关键字：" class="headerlink" title="（1）关键字："></a>（1）关键字：</h6><ul>
<li><p> begin  if  then  while  do  end</p>
</li>
<li><p> 所有的关键字都是小写。</p>
<h6 id="（2）运算符和界符"><a href="#（2）运算符和界符" class="headerlink" title="（2）运算符和界符"></a>（2）运算符和界符</h6></li>
<li><p>： =  +  -  *  /  &lt;  &lt;=  &lt;&gt;  &gt;  &gt;=  =  ; (  )  #</p>
</li>
</ul>
<h6 id="（3）其他单词是标识符（ID）和整型常数（SUM），通过以下正规式定义："><a href="#（3）其他单词是标识符（ID）和整型常数（SUM），通过以下正规式定义：" class="headerlink" title="（3）其他单词是标识符（ID）和整型常数（SUM），通过以下正规式定义："></a>（3）其他单词是标识符（ID）和整型常数（SUM），通过以下正规式定义：</h6><ul>
<li>ID = letter (letter | digit)*</li>
<li>NUM = digit digit*<h6 id="（4）空格有空白、制表符和换行符组成。空格一般用来分隔ID、SUM、运算符、界符和关键字，词法分析阶段通常被忽略。"><a href="#（4）空格有空白、制表符和换行符组成。空格一般用来分隔ID、SUM、运算符、界符和关键字，词法分析阶段通常被忽略。" class="headerlink" title="（4）空格有空白、制表符和换行符组成。空格一般用来分隔ID、SUM、运算符、界符和关键字，词法分析阶段通常被忽略。"></a>（4）空格有空白、制表符和换行符组成。空格一般用来分隔ID、SUM、运算符、界符和关键字，词法分析阶段通常被忽略。</h6><h5 id="2-2-各种单词符号对应的种别码："><a href="#2-2-各种单词符号对应的种别码：" class="headerlink" title="2.2 各种单词符号对应的种别码："></a>2.2 各种单词符号对应的种别码：</h5></li>
</ul>
<p> <strong>表2.1 各种单词符号对应的种别码</strong> </p>
<table>
<thead>
<tr>
<th>单词符号</th>
<th>种别码</th>
<th>单词符号</th>
<th>种别码</th>
</tr>
</thead>
<tbody><tr>
<td>begin</td>
<td>1</td>
<td>：</td>
<td>17</td>
</tr>
<tr>
<td>if</td>
<td>2</td>
<td>：=</td>
<td>18</td>
</tr>
<tr>
<td>then</td>
<td>3</td>
<td>&lt;</td>
<td>20</td>
</tr>
<tr>
<td>while</td>
<td>4</td>
<td>&lt;&gt;</td>
<td>21</td>
</tr>
<tr>
<td>do</td>
<td>5</td>
<td>&lt;=</td>
<td>22</td>
</tr>
<tr>
<td>end</td>
<td>6</td>
<td>&gt;</td>
<td>23</td>
</tr>
<tr>
<td>lettet（letter / digit）*</td>
<td>10</td>
<td>&gt;=</td>
<td>24</td>
</tr>
<tr>
<td>dight dight*</td>
<td>11</td>
<td>=</td>
<td>25</td>
</tr>
</tbody></table>
<ul>
<li>|   13  |； |   26<br>— |   14 |   (  |  27</li>
</ul>
<ul>
<li> |  15 |   ) |   28<br>/  |  16 |   # |   0<h5 id="2-3-词法分析程序的功能："><a href="#2-3-词法分析程序的功能：" class="headerlink" title="2.3 词法分析程序的功能："></a>2.3 词法分析程序的功能：</h5>输入：所给文法的源程序字符串。<br>输出：二元组（syn,token或sum）构成的序列。<br>其中：</li>
</ul>
<ul>
<li>syn为单词种别码；</li>
<li>token为存放的单词自身字符串；</li>
<li>sum为整型常数;</li>
<li>例如：对源程序begin x:=9: if x&gt;9 then x:=2*x+1/3; end #的源文件，经过词法分析后输出如下序列：<br>(1,begin)(10,x)(18,:=)(11,9)(26, ; )(2,if)……</li>
</ul>
<h4 id="设计思路："><a href="#设计思路：" class="headerlink" title="设计思路："></a>设计思路：</h4><p><strong>Judge函数</strong>：<br>1.对所给的单词（字符数组）做出判断：</p>
<ul>
<li>关键字，运算符和界符，标识符（ID），整型常数（SUM）：</li>
</ul>
<p>2.读到的字符串处理方法：<br>首先判断传入参数的第一个字符，分以下几种情况：</p>
<ul>
<li>字母开头：判断是否为关键字，是则返回，不是则为标识符</li>
<li>数字开头的判断是否全为数字 是则处理后返回十进制数字大小，不是则返回错误</li>
<li>其他情况直接和运算符表做对比 是则返回运算符，不是则返回错误</li>
</ul>
<p>3.判断后返回种别码<br><strong>main（）函数</strong></p>
<ul>
<li>读取文件（待分析的源代码）</li>
<li>从文件中取出一个个独立的单词（以分隔符为界限）</li>
<li>把单词作为参数调用Judge() 返回值对应的种别码</li>
<li>用上述得到的种别码和单词为参数调用print函数，输出结果到文件</li>
</ul>
<p><strong>Print函数</strong>：参数为单词和种别码</p>
<ul>
<li>写文件模式为追加写（每次在文件末尾写，不覆原有内容）（ios::app）</li>
</ul>
<h4 id="代码实现："><a href="#代码实现：" class="headerlink" title="代码实现："></a>代码实现：</h4><figure class="highlight cpp"><table><tr><td class="gutter"><div class="code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br></pre></div></td><td class="code"><pre><code class="hljs cpp"><span class="hljs-meta">#<span class="hljs-keyword">include</span><span class="hljs-string">&lt;iostream&gt;</span></span><br><span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string">&lt;fstream&gt;</span></span><br><br><span class="hljs-keyword">using</span> <span class="hljs-keyword">namespace</span> std;<br><br><span class="hljs-type">const</span> <span class="hljs-type">int</span> symbolNum =<span class="hljs-number">29</span>;<br><span class="hljs-type">char</span> syn[symbolNum+<span class="hljs-number">1</span>][<span class="hljs-number">10</span>] &#123; <br>	<span class="hljs-string">&quot;#&quot;</span>,<span class="hljs-string">&quot;begin&quot;</span>,<span class="hljs-string">&quot;if&quot;</span>,<span class="hljs-string">&quot;then&quot;</span>,<span class="hljs-string">&quot;while&quot;</span>,<span class="hljs-string">&quot;do&quot;</span>,<span class="hljs-string">&quot;end&quot;</span>,<span class="hljs-string">&quot;&quot;</span>,<span class="hljs-string">&quot;&quot;</span>,<span class="hljs-string">&quot;&quot;</span>,<br>	<span class="hljs-string">&quot;token&quot;</span>,<span class="hljs-string">&quot;sum&quot;</span>,<span class="hljs-string">&quot;&quot;</span>,<span class="hljs-string">&quot;+&quot;</span>,<span class="hljs-string">&quot;-&quot;</span>,<span class="hljs-string">&quot;*&quot;</span>,<span class="hljs-string">&quot;/&quot;</span> ,<span class="hljs-string">&quot;:&quot;</span> ,<span class="hljs-string">&quot;:=&quot;</span>,<span class="hljs-string">&quot;&quot;</span> ,<br>	<span class="hljs-string">&quot;&lt;&quot;</span> ,<span class="hljs-string">&quot;&lt;&gt;&quot;</span> ,<span class="hljs-string">&quot;&lt;=&quot;</span> ,<span class="hljs-string">&quot;&gt;&quot;</span> ,<span class="hljs-string">&quot;&gt;=&quot;</span> ,<span class="hljs-string">&quot;=&quot;</span> ,<span class="hljs-string">&quot;;&quot;</span> ,<span class="hljs-string">&quot;(&quot;</span> ,<span class="hljs-string">&quot;)&quot;</span> ,<span class="hljs-string">&quot;Error!!!&quot;</span><br>&#125;;<br><br><span class="hljs-function"><span class="hljs-type">char</span>* <span class="hljs-title">GetToken</span><span class="hljs-params">()</span> </span>&#123;<br>	<span class="hljs-function">ifstream <span class="hljs-title">In</span><span class="hljs-params">(<span class="hljs-string">&quot;输入.txt&quot;</span>)</span></span>;<br>	<span class="hljs-type">char</span>* A = <span class="hljs-keyword">new</span> <span class="hljs-type">char</span>[<span class="hljs-number">10</span>];<br>	<span class="hljs-keyword">for</span> (<span class="hljs-type">int</span> i = <span class="hljs-number">0</span>; i &lt; <span class="hljs-number">10</span>; i++)<br>		A[i] = <span class="hljs-string">&#x27;\0&#x27;</span>;<br>	In &gt;&gt; A;<br><br>	<span class="hljs-comment">//cout &lt;&lt; &quot;\n读入&quot; &lt;&lt; A &lt;&lt; endl;</span><br><br>	In.<span class="hljs-built_in">close</span>();<br>	<span class="hljs-keyword">return</span> A;<br>&#125;<br><br><span class="hljs-function"><span class="hljs-type">bool</span> <span class="hljs-title">IsLetter</span><span class="hljs-params">(<span class="hljs-type">char</span> a)</span> </span>&#123;<br>	<span class="hljs-keyword">if</span> ((a &gt;= <span class="hljs-string">&#x27;a&#x27;</span> &amp;&amp; a &lt;= <span class="hljs-string">&#x27;z&#x27;</span>)|| (a &gt;= <span class="hljs-string">&#x27;A&#x27;</span> &amp;&amp; a &lt;= <span class="hljs-string">&#x27;Z&#x27;</span>))<br>		<span class="hljs-keyword">return</span> <span class="hljs-number">1</span>;<br>	<span class="hljs-keyword">else</span> <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;<br>&#125;<br><br><span class="hljs-function"><span class="hljs-type">bool</span> <span class="hljs-title">IsDigit</span><span class="hljs-params">(<span class="hljs-type">char</span> a)</span> </span>&#123;<br>	<span class="hljs-keyword">if</span> (a &gt;= <span class="hljs-string">&#x27;0&#x27;</span> &amp;&amp; a &lt;= <span class="hljs-string">&#x27;9&#x27;</span>)<br>		<span class="hljs-keyword">return</span> <span class="hljs-number">1</span>;<br>	<span class="hljs-keyword">else</span> <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;<br>&#125;<br><br><span class="hljs-function"><span class="hljs-type">int</span> <span class="hljs-title">Judge</span><span class="hljs-params">(<span class="hljs-type">char</span> Token[])</span> </span>&#123;<br>	<span class="hljs-type">char</span> beg = Token[<span class="hljs-number">0</span>];<br>	<span class="hljs-keyword">if</span> (<span class="hljs-built_in">IsLetter</span>(beg)) &#123;<span class="hljs-comment">//字母开头</span><br>		<span class="hljs-keyword">for</span> (<span class="hljs-type">int</span> i = <span class="hljs-number">0</span>; Token[i] != <span class="hljs-string">&#x27;\0&#x27;</span>; i++)<br>			<span class="hljs-keyword">if</span> (!<span class="hljs-built_in">IsDigit</span>(Token[i]) &amp;&amp; !<span class="hljs-built_in">IsLetter</span>(Token[i]))<br>				<span class="hljs-keyword">return</span> symbolNum;<span class="hljs-comment">//返回错误</span><br><br>		<span class="hljs-keyword">for</span> (<span class="hljs-type">int</span> i = <span class="hljs-number">0</span>; i &lt; <span class="hljs-number">7</span>; i++) &#123;<br>			<span class="hljs-keyword">if</span> (beg == syn[i][<span class="hljs-number">0</span>]) &#123;<br>				<span class="hljs-keyword">for</span> (<span class="hljs-type">int</span> j = <span class="hljs-number">0</span>; Token[j] != <span class="hljs-string">&#x27;\0&#x27;</span>&amp;&amp; syn[i][j] != <span class="hljs-string">&#x27;\0&#x27;</span>; j++) &#123;<br>				<span class="hljs-comment">//for (int j = 0; ; j++) &#123;</span><br>					<span class="hljs-keyword">if</span> (Token[j + <span class="hljs-number">1</span>] != syn[i][j + <span class="hljs-number">1</span>])<br>						<span class="hljs-keyword">return</span> <span class="hljs-number">10</span>;<span class="hljs-comment">//是标识符</span><br>				&#125;<br>				<span class="hljs-keyword">return</span> i;<span class="hljs-comment">//是关键字</span><br>			&#125;<br>		&#125;<br>		<span class="hljs-keyword">return</span> <span class="hljs-number">10</span>;<span class="hljs-comment">//是标识符</span><br>	&#125;<br>	<span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (<span class="hljs-built_in">IsDigit</span>(beg)) &#123;<span class="hljs-comment">//数字开头</span><br>		<span class="hljs-keyword">for</span> (<span class="hljs-type">int</span> i = <span class="hljs-number">0</span>; Token[i] != <span class="hljs-string">&#x27;\0&#x27;</span>; i++) &#123;<br>			<span class="hljs-keyword">if</span> (!<span class="hljs-built_in">IsDigit</span>(Token[i]))<br>				<span class="hljs-keyword">return</span> symbolNum;<span class="hljs-comment">//返回错误（数字开头不全为数字）</span><br>		&#125;<br>		<span class="hljs-keyword">return</span> <span class="hljs-number">11</span>;<span class="hljs-comment">//是数字</span><br>	&#125;<br>	<span class="hljs-keyword">else</span> &#123;<span class="hljs-comment">//符号开头</span><br>		<span class="hljs-keyword">if</span> (beg == <span class="hljs-string">&#x27;#&#x27;</span> &amp;&amp; Token[<span class="hljs-number">1</span>] == <span class="hljs-string">&#x27;\0&#x27;</span>)<br>			<span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;<br>		<span class="hljs-keyword">for</span> (<span class="hljs-type">int</span> i = <span class="hljs-number">13</span>; i &lt; symbolNum; i++) &#123;<br>			<span class="hljs-keyword">if</span> (beg == syn[i][<span class="hljs-number">0</span>]) &#123;<br>				<span class="hljs-type">bool</span> flag = <span class="hljs-number">1</span>;<br>				<span class="hljs-keyword">for</span> (<span class="hljs-type">int</span> j = <span class="hljs-number">0</span>; Token[j] != <span class="hljs-string">&#x27;\0&#x27;</span> &amp;&amp; syn[i][j] != <span class="hljs-string">&#x27;\0&#x27;</span>; j++) &#123;<br>				<span class="hljs-comment">//for (int j = 0; ; j++) &#123;</span><br>					<span class="hljs-keyword">if</span> (Token[j + <span class="hljs-number">1</span>] != syn[i][j + <span class="hljs-number">1</span>]) &#123;<br>						flag = <span class="hljs-number">0</span>;<br>						<span class="hljs-keyword">break</span>;<br>					&#125;<br>				&#125;<br>				<span class="hljs-keyword">if</span>(flag)<br>					<span class="hljs-keyword">return</span> i;<span class="hljs-comment">//是运算符</span><br>			&#125;<br>		&#125;<br>		<span class="hljs-keyword">return</span> symbolNum;<span class="hljs-comment">//返回错误</span><br>	&#125;<br>&#125;<br><br><span class="hljs-function"><span class="hljs-type">void</span> <span class="hljs-title">C_Print</span><span class="hljs-params">(<span class="hljs-type">int</span> id, <span class="hljs-type">char</span>* Token)</span> </span>&#123;<br><span class="hljs-comment">//输出到文件</span><br><br>	ofstream Out;<br>    Out.<span class="hljs-built_in">open</span>(<span class="hljs-string">&quot;输出.txt&quot;</span>, ios::app);<br>	<span class="hljs-keyword">if</span> (id == symbolNum) Out &lt;&lt; <span class="hljs-string">&quot;Error  &quot;</span>;<br>	Out &lt;&lt; <span class="hljs-string">&quot;(&quot;</span> &lt;&lt; id &lt;&lt; <span class="hljs-string">&quot;,&quot;</span>;<br>	<span class="hljs-keyword">for</span> (<span class="hljs-type">int</span> i = <span class="hljs-number">0</span>; Token[i] != <span class="hljs-string">&#x27;\0&#x27;</span>; i++)<br>		Out &lt;&lt; Token[i];<br>	Out &lt;&lt; <span class="hljs-string">&quot;)&quot;</span> &lt;&lt; endl;<br><br>	Out.<span class="hljs-built_in">close</span>();<br><br><span class="hljs-comment">//以下为输出到控制台</span><br>	<span class="hljs-keyword">if</span> (id == symbolNum)cout &lt;&lt; <span class="hljs-string">&quot;Error  &quot;</span>;<br>	cout &lt;&lt; <span class="hljs-string">&quot;(&quot;</span> &lt;&lt; id &lt;&lt; <span class="hljs-string">&quot;,&quot;</span>;<br>	<span class="hljs-keyword">for</span> (<span class="hljs-type">int</span> i = <span class="hljs-number">0</span>; Token[i] != <span class="hljs-string">&#x27;\0&#x27;</span>; i++)<br>		cout &lt;&lt; Token[i];<br>	cout &lt;&lt; <span class="hljs-string">&quot;)&quot;</span> &lt;&lt; endl;<br>&#125;<br><br><span class="hljs-function"><span class="hljs-type">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span> </span>&#123;<br><br>	<span class="hljs-function">ifstream <span class="hljs-title">In</span><span class="hljs-params">(<span class="hljs-string">&quot;输入.txt&quot;</span>)</span></span>;<br>	<span class="hljs-function">ofstream <span class="hljs-title">Out</span><span class="hljs-params">(<span class="hljs-string">&quot;输出.txt&quot;</span>, ios::trunc)</span></span>;<span class="hljs-comment">//清除之前的内容</span><br><br>	<span class="hljs-keyword">while</span> (!In.<span class="hljs-built_in">eof</span>()) &#123;<br>		<span class="hljs-type">char</span>* A = <span class="hljs-keyword">new</span> <span class="hljs-type">char</span>[<span class="hljs-number">10</span>];<br>		<span class="hljs-keyword">for</span> (<span class="hljs-type">int</span> i = <span class="hljs-number">0</span>; i &lt; <span class="hljs-number">10</span>; i++)<br>			A[i] = <span class="hljs-string">&#x27;\0&#x27;</span>;<br>		In &gt;&gt; A;<br><br>		<span class="hljs-type">int</span> id = <span class="hljs-built_in">Judge</span>(A);<br>		<span class="hljs-built_in">C_Print</span>(id, A);	<br>	&#125;<br>	In.<span class="hljs-built_in">close</span>();<br><br><br>	<span class="hljs-comment">//char* T = GetToken();</span><br>	<span class="hljs-comment">//	int id = Judge(T);</span><br>	<span class="hljs-comment">//	C_Print(id, T);</span><br>&#125;<br></code></pre></td></tr></table></figure>
<p>文件：输出.txt 的内容（随便写的，测试用）</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><code class="hljs cpp"><span class="hljs-keyword">if</span> ( a &gt; <span class="hljs-number">0</span> )<br>	<span class="hljs-keyword">while</span> ( a &gt;= <span class="hljs-number">9</span> ) :<br>		a = a - <span class="hljs-number">1</span> ;<br><span class="hljs-meta">#</span><br><span class="hljs-meta"><span class="hljs-keyword">else</span> &#123; </span><br>	<span class="hljs-keyword">if</span> ( b &lt; <span class="hljs-number">987</span> ) :<br>		b ++ ;<br>&#125; end <br></code></pre></td></tr></table></figure>
<p>输出结果：<br><img src="https://imgbed.cheney.cc/picgo/2021040515355729.png" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"></p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br></pre></td><td class="code"><pre><code class="hljs cpp"><span class="hljs-comment">//from 输出.txt</span><br><br>(<span class="hljs-number">2</span>,<span class="hljs-keyword">if</span>)<br>(<span class="hljs-number">27</span>,()<br>(<span class="hljs-number">10</span>,a)<br>(<span class="hljs-number">23</span>,&gt;)<br>(<span class="hljs-number">11</span>,<span class="hljs-number">0</span>)<br>(<span class="hljs-number">28</span>,))<br>(<span class="hljs-number">4</span>,<span class="hljs-keyword">while</span>)<br>(<span class="hljs-number">27</span>,()<br>(<span class="hljs-number">10</span>,a)<br>(<span class="hljs-number">24</span>,&gt;=)<br>(<span class="hljs-number">11</span>,<span class="hljs-number">9</span>)<br>(<span class="hljs-number">28</span>,))<br>(<span class="hljs-number">17</span>,:)<br>(<span class="hljs-number">10</span>,a)<br>(<span class="hljs-number">25</span>,=)<br>(<span class="hljs-number">10</span>,a)<br>(<span class="hljs-number">14</span>,-)<br>(<span class="hljs-number">11</span>,<span class="hljs-number">1</span>)<br>(<span class="hljs-number">26</span>,;)<br>(<span class="hljs-number">0</span>,#)<br>(<span class="hljs-number">10</span>,<span class="hljs-keyword">else</span>)<br><span class="hljs-built_in">Error</span>  (<span class="hljs-number">29</span>,&#123;)<br>(<span class="hljs-number">2</span>,<span class="hljs-keyword">if</span>)<br>(<span class="hljs-number">27</span>,()<br>(<span class="hljs-number">10</span>,b)<br>(<span class="hljs-number">20</span>,&lt;)<br>(<span class="hljs-number">11</span>,<span class="hljs-number">987</span>)<br>(<span class="hljs-number">28</span>,))<br>(<span class="hljs-number">17</span>,:)<br>(<span class="hljs-number">10</span>,b)<br><span class="hljs-built_in">Error</span>  (<span class="hljs-number">29</span>,++)<br>(<span class="hljs-number">26</span>,;)<br><span class="hljs-built_in">Error</span>  (<span class="hljs-number">29</span>,&#125;)<br>(<span class="hljs-number">6</span>,end)<br>(<span class="hljs-number">19</span>,)<br></code></pre></td></tr></table></figure>


            </div>
            <hr>
            <div>
              <div class="post-metas mb-3">
                
                  <div class="post-meta mr-3">
                    <i class="iconfont icon-category"></i>
                    
                      <a class="hover-with-bg" href="/categories/%E8%AE%A1%E7%AE%97%E6%9C%BA%E5%9F%BA%E7%A1%80/">计算机基础</a>
                    
                  </div>
                
                
                  <div class="post-meta">
                    <i class="iconfont icon-tags"></i>
                    
                      <a class="hover-with-bg" href="/tags/C/">C++</a>
                    
                      <a class="hover-with-bg" href="/tags/%E7%BC%96%E8%AF%91%E5%8E%9F%E7%90%86/">编译原理</a>
                    
                  </div>
                
              </div>
              
                <p class="note note-warning">
                  
                    本博客所有文章除特别声明外，均采用 <a target="_blank" href="https://creativecommons.org/licenses/by-sa/4.0/deed.zh" rel="nofollow noopener noopener">CC BY-SA 4.0 协议</a> ，转载请注明出处！
                  
                </p>
              
              
                <div class="post-prevnext">
                  <article class="post-prev col-6">
                    
                    
                      <a href="/2021/04/19/%E7%BC%96%E8%AF%91%E5%8E%9F%E7%90%86__%E5%9F%BA%E4%BA%8EDFA%E7%9A%84%E8%AF%8D%E6%B3%95%E5%88%86%E6%9E%90%EF%BC%88%E4%B8%80%EF%BC%89%EF%BC%9A%E7%A8%8B%E5%BA%8F%E6%A1%86%E6%9E%B6%E5%8F%8A%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/">
                        <i class="iconfont icon-arrowleft"></i>
                        <span class="hidden-mobile">基于DFA的词法分析（一）：程序框架及数据结构</span>
                        <span class="visible-mobile">上一篇</span>
                      </a>
                    
                  </article>
                  <article class="post-next col-6">
                    
                    
                      <a href="/2020/12/09/%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84%E8%AF%BE%E8%AE%BE--%E4%BA%A4%E9%80%9A%E5%92%A8%E8%AF%A2%E7%B3%BB%E7%BB%9F%E8%AE%BE%E8%AE%A1/">
                        <span class="hidden-mobile">交通咨询系统</span>
                        <span class="visible-mobile">下一篇</span>
                        <i class="iconfont icon-arrowright"></i>
                      </a>
                    
                  </article>
                </div>
              
            </div>

            
          </article>
        </div>
      </div>
    </div>
    
      <div class="d-none d-lg-block col-lg-2 toc-container" id="toc-ctn">
        <div id="toc">
  <p class="toc-header"><i class="iconfont icon-list"></i>&nbsp;目录</p>
  <div class="toc-body" id="toc-body"></div>
</div>

      </div>
    
  </div>
</div>

<!-- Custom -->


    

    
      <a id="scroll-top-button" aria-label="TOP" href="#" role="button">
        <i class="iconfont icon-arrowup" aria-hidden="true"></i>
      </a>
    

    
      <div class="modal fade" id="modalSearch" tabindex="-1" role="dialog" aria-labelledby="ModalLabel"
     aria-hidden="true">
  <div class="modal-dialog modal-dialog-scrollable modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header text-center">
        <h4 class="modal-title w-100 font-weight-bold">搜索</h4>
        <button type="button" id="local-search-close" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body mx-3">
        <div class="md-form mb-5">
          <input type="text" id="local-search-input" class="form-control validate">
          <label data-error="x" data-success="v"
                 for="local-search-input">关键词</label>
        </div>
        <div class="list-group" id="local-search-result"></div>
      </div>
    </div>
  </div>
</div>
    

    
  </main>

  <footer class="text-center mt-5 py-3">
  <div class="footer-content">
     <a href="https://hexo.io" target="_blank" rel="nofollow noopener"><span>Hexo</span></a> <i class="iconfont icon-love"></i> <a href="https://cheney822.gitee.io/" target="_blank" rel="nofollow noopener"><span>备用网址</span></a> 
  </div>
  

  
  <!-- 备案信息 -->
  <div class="beian">
    <span>
      <a href="http://beian.miit.gov.cn/" target="_blank" rel="nofollow noopener">
        皖ICP备2022002876号-1
      </a>
    </span>
    
  </div>


  
</footer>


  <!-- SCRIPTS -->
  
  <script  src="https://cdn.jsdelivr.net/npm/nprogress@0/nprogress.min.js" ></script>
  <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/nprogress@0/nprogress.min.css" />

  <script>
    NProgress.configure({"showSpinner":false,"trickleSpeed":100})
    NProgress.start()
    window.addEventListener('load', function() {
      NProgress.done();
    })
  </script>


<script  src="https://cdn.jsdelivr.net/npm/jquery@3/dist/jquery.min.js" ></script>
<script  src="https://cdn.jsdelivr.net/npm/bootstrap@4/dist/js/bootstrap.min.js" ></script>
<script  src="/js/events.js" ></script>
<script  src="/js/plugins.js" ></script>

<!-- Plugins -->


  <script  src="/js/local-search.js" ></script>



  
    <script  src="/js/img-lazyload.js" ></script>
  



  



  
    <script  src="https://cdn.jsdelivr.net/npm/tocbot@4/dist/tocbot.min.js" ></script>
  
  
    <script  src="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3/dist/jquery.fancybox.min.js" ></script>
  
  
    <script  src="https://cdn.jsdelivr.net/npm/anchor-js@4/anchor.min.js" ></script>
  
  
    <script defer src="https://cdn.jsdelivr.net/npm/clipboard@2/dist/clipboard.min.js" ></script>
  






  <script  src="https://cdn.jsdelivr.net/npm/typed.js@2/lib/typed.min.js" ></script>
  <script>
    (function (window, document) {
      var typing = Fluid.plugins.typing;
      var title = document.getElementById('subtitle').title;
      
        typing(title);
      
    })(window, document);
  </script>















<!-- 主题的启动项 保持在最底部 -->
<script  src="/js/boot.js" ></script>


</body>
</html>
